package acom.test.swing.aa;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Iterator;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class RunPanel extends JPanel{
	
	private static final long serialVersionUID = 1L;
	
	private JLabel labelTop;
	private JLabel labelMiddle;
	private JLabel labelBottom;
	private boolean Gaming = false;
	
	static List<AnswerPeople> peoples;
	static List<Prize> prizes;
	
	private Prize currentPrize; //当前抽取的奖项
		
	private JTextArea textarea;
	
	public RunPanel(JTextArea text){
		this.textarea = text;
		this.setLayout(new GridLayout(0, 1));
        labelTop = addLabel(false);
    	labelMiddle =  addLabel(true);
    	labelBottom = addLabel(false);
    	
	}
	
	
	private JLabel addLabel(boolean icon) {
		JPanel panel = new JPanel(new BorderLayout());
		panel.setOpaque(false);
		panel.add(new JLabel(new ArrowIcon(icon)), BorderLayout.WEST);
		JLabel label = new JLabel();
		label.setHorizontalAlignment(JLabel.CENTER);
		label.setFont(new Font("宋体",Font.PLAIN,28));
		panel.add(label);
		add(panel);
		return label;
	}
	
	public void moveLable(final String caption) {
		try {
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					labelTop.setText(labelMiddle.getText());
					labelMiddle.setText(labelBottom.getText());
					labelBottom.setText(caption);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void start() {
		Gaming = true;
		new Thread(new GameDriver()).start();
	}

	public void stop() {
		Gaming = false;
	}

	public boolean isRunning() {
		return Gaming;
	}
	
	class GameDriver implements Runnable {

		@Override
		public void run() {
			prizes = SystemUtil.getPrizelist();
			peoples=SystemUtil.getPeoplelist();
			
			if(prizes.size()==0){
				Gaming = false;
				JOptionPane.showMessageDialog(null,"还没有设置奖项，请先设置奖项");
				return;
			}
			
			currentPrize=SystemUtil.getCurPrize();			
			List<AnswerPeople> currentPrize_peoples=currentPrize.getPersons();
						
			
			if(currentPrize.getIsFinish()|| (currentPrize_peoples.size()==currentPrize.getTotal()) ){
				Gaming = false;
				JOptionPane.showMessageDialog(null,currentPrize.getPname()+",已抽完 请选择别的奖项");
				return;
			}
			if(peoples.size()<=1){
				Gaming = false;
				JOptionPane.showMessageDialog(null,"亲!! 候选人数不够了");
				return;
			}
			
			
			while (Gaming) {
				for (Iterator<AnswerPeople> it = peoples.iterator(); it.hasNext()
						&& Gaming;) {
					moveLable(it.next().getPeopleName());
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
			}
			
			System.out.println("抽奖 抽中的人:"+labelMiddle.getText());
			
			//记录获奖者
			AnswerPeople awardPeople = getPeopleByName(labelMiddle.getText());
			currentPrize_peoples.add(awardPeople);
			if(currentPrize_peoples.size()==currentPrize.getTotal()){
				currentPrize.setIsFinish(true);
				
			}
			
			//打印 获奖人名单
			printPrizeResult();
			
			//抽完奖后，把 获奖者从名单中去掉
			removePeople(awardPeople);
									
		}
	}
	
	public AnswerPeople getPeopleByName(String pname){
		AnswerPeople cur_pp=null;
		peoples=SystemUtil.getPeoplelist();
		for(int i=0;i<peoples.size();i++){
			String sname=peoples.get(i).getPeopleName();
			if(pname.equals(sname)){
				cur_pp=peoples.get(i);
			}
		}
		
		return cur_pp;
	}
	
	//抽完奖后，把 获奖者从名单中去掉
	public void removePeople(AnswerPeople people){
		
		if(null!=people){
			peoples=SystemUtil.getPeoplelist();
			peoples.remove(people);
			
			System.out.println("去除 获奖者："+people.getPeopleName());
		}
		
	}
	
	//抽奖完后，打印抽奖结果
	public void printPrizeResult(){
		textarea.setText("");
		textarea.append("中奖结果：" + "\r\n");
		
		for(Prize prize: prizes){
			List<AnswerPeople> prize_peoples=prize.getPersons();
			if(prize_peoples.size()>0){
				//奖名称
				textarea.append(prize.getPname() +"(获奖者如下：)"+ "\r\n");
							
				//该奖项下，获奖人名称			
				for(AnswerPeople pp: prize_peoples){
					textarea.append(pp.getPeopleName() + "\r\n");
				}
			}
			
			
		}
	}
	
}
